package com.example.picturestorage.interceptor;

import com.example.picturestorage.bean.Publisher;
import com.example.picturestorage.controller.PublisherController;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets",
        args = {Statement.class})})
public class MyBatisInterceptor implements Interceptor {

    private final static Logger LOGGER = LoggerFactory.getLogger(MyBatisInterceptor.class);
    @Override
    public Object intercept(Invocation invocation) throws InvocationTargetException, IllegalAccessException {
        LOGGER.info("---------------------------------------------------------");
        LOGGER.info("getTarget " + invocation.getTarget());
        LOGGER.info("getMethod " + invocation.getMethod());
        LOGGER.info("getArgs " + Arrays.toString(invocation.getArgs()));
        LOGGER.info("---------------------------------------------------------");
        List<Object> proceed = (List<Object>) invocation.proceed();
        LOGGER.info("proceed " + proceed);
        for (Object o : proceed
        ) {
            if (o instanceof Publisher) {
                LOGGER.info("o instanceof Publisher " + true);
                Publisher publisher = (Publisher) o;
                LOGGER.info(String.valueOf(publisher));
            }
        }
        return proceed;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
